package com.quwan.manager.controller;

import com.quwan.app.service.ProductCategoryService;
import com.quwan.common.BaseController;
import com.quwan.common.result.ApiResult;
import com.quwan.common.result.PageResult;
import com.quwan.entity.TProductCategory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * @author quan
 * @date 2021-08-03 14:07
 */
@Api(tags = "服务分类接口")
@RestController
@RequestMapping("mgr/cate")
@RequiredArgsConstructor
public class CategoryTagController extends BaseController {
    private final ProductCategoryService categoryService;

    @ApiOperation("分类标签列表")
    @PostMapping("list")
    public ApiResult<PageResult<TProductCategory>> categoryTagList(@RequestParam(defaultValue = "1") int page,
                                                                   @RequestParam(defaultValue = "10") int size,
                                                                   @ApiParam("分类id") @RequestParam(required = false) String id,
                                                                   @ApiParam("分类名称") @RequestParam(required = false) String caption,
                                                                   @ApiParam("分类类型") @RequestParam(required = false) Integer cateType,
                                                                   @ApiParam("开始时间") @RequestParam(required = false) String beginTime,
                                                                   @ApiParam("结束时间") @RequestParam(required = false) String endTime) {
        PageResult<TProductCategory> res = categoryService.getAllCateTag(page, size, id, caption,cateType, beginTime, endTime);
        return ApiResult.success(res);

    }

    @ApiOperation("一级分类标签 查询")
    @PostMapping("parentTag")
    public ApiResult<List<TProductCategory>> parentTag() {
        return ApiResult.success(categoryService.getParentTag());

    }

    @PostMapping("updCateTag")
    @ApiOperation("标签修改")
    public ApiResult updCateTag(HttpServletRequest request, TProductCategory category) {
        TokenInfo tokenInfo = getTokenInfo(request);
        category.setUpdateBy(tokenInfo.getUsername());
        boolean success = categoryService.updCateTag(category);
        if (success) return ApiResult.success();
        else return ApiResult.failed();
    }

    @PostMapping("addCateTag")
    @ApiOperation("标签添加")
    public ApiResult addCateTag(HttpServletRequest request, TProductCategory category) {
        TokenInfo tokenInfo = getTokenInfo(request);
        category.setAddBy(tokenInfo.getUsername());
        boolean success = categoryService.addCateTag(category);
        if (success) return ApiResult.success();
        else return ApiResult.failed();
    }

    @PostMapping("delCateTag")
    @ApiOperation("标签删除")
    public ApiResult delCateTag(HttpServletRequest request,
                                @ApiParam("分类id") @RequestParam(required = true) Long id) {

        boolean success = categoryService.delCateTag(id);
        if (success) return ApiResult.success();
        else return ApiResult.failed();
    }
}
